const app = getApp();
var interval = null //倒计时函数
Page({
  data:{
    state: false,
    showModalStatus: false,
    taxshow: false,    //税
    tax:0,
    test1: '',
    test: '',
    time: '获取验证码',
    currentTime: 61,
    money:'',
    disabled1: true,
    // disabled:true,
    nameA:''
  },
  getTest: function () {
    this.setData({
      test1: '已向' + this.data.phoneNumber + '的手机号发送验证码'
    })
    console.log(this.data.test1);
    this.setData({
      testStatu: true,
      test: this.data.test1
    })
    if (!interval){
      this.sendCode();
    }
  },
  toResult: function (e) {
    const that = this;
    console.log(e.detail.value.money);
    if (!e.detail.value.money){
      wx.showModal({
        content: '请输入余额',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          }
        }
      });
    } else if (e.detail.value.money > this.data.balanceBefore) {
      wx.showModal({
        content: '余额不足',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          }
        }
      });
    } else if (parseFloat(e.detail.value.money > 50000)){
      wx.showModal({
        content: '提现金额不能超过5万',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          }
        }
      });
    }else if (e.detail.value.money <= that.data.balanceBefore) {
      this.setData({
        showModalStatus: true
      })
      that.getTest();
      console.log('触发验证码');
    }
  },
  onShow:function(){
    this.getuserinfo();
  },
  onLoad:function(options){
    console.log(options);
    this.setData({
      disabled1: true
    })
    if(options.from == 'transferother'){
      this.setData({
        taxshow:false,
        bankId:options.bankId,
        nameA:'转账'
      })
      wx.setNavigationBarTitle({
        title: '转账'
      })
    }else{
      this.setData({
        taxshow:true,
        bankId: options.bankId,
        nameA: '打款'
      })
      wx.setNavigationBarTitle({
        title: '打款'
      })
    }
    // this.getuserinfo();
  },
  // 验证两位小数
  tzMoney(e) {
    console.log(e.detail.value);
    let val = e.detail.value;
    // let re = /^(\-)*(\d+)\.(\d\d).*$/;
    let re = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;
    if(e.detail.value){
      if (e.detail.value != '.') {
        if (!re.test(e.detail.value)) {
          let a = this.clearNoNum(val);
          this.setData({
            money: a
          })
        } else {
          this.setData({
            money: e.detail.value
          })
        }
        this.getTax();
      } else {
        let a = this.clearNoNum('0' + val);
        console.log(a);
        this.setData({
          money: a
        })
      }
    }else{
      this.setData({
        tax: 0
      })
    }
  },
  // 两位小数
  clearNoNum(value) {
    value = value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符  
    value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的  
    value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数  
    if (value.indexOf(".") < 0 && value != "") {
      value = parseFloat(value);
    }
    return value;
  },
  // 获得用户信息
  getuserinfo:function(){
    const _this = this;
    // 用户信息
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetOtherUserInfo',
      data: {
        phoneNumOrIdentityCard: _this.data.bankId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          if(res.data.result.length == 1){
            _this.setData({
              name: res.data.result[0].name,
              otherId: res.data.result[0].id,
              phoneNumber1: res.data.result[0].phoneNumber,
              identityCard: res.data.result[0].identityCard,
              PicturePath: res.data.result[0].profilePicturePath
            })
          }
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
     // 自己的
    let baseInfo = app.globalData.baseInfo;
    _this.setData({
      balance: _this.numFilter(baseInfo.balance),
      balanceBefore: baseInfo.balance,
      phoneNumber: baseInfo.phoneNumber,
      phoneNumberAll: baseInfo.userName,
    })
  },
  // 关闭弹框
  closemodal: function () {
    this.setData({
      showModalStatus: false
    })
    // clearInterval(interval)
  },
  //money
  getmoney: function (e) {
    if (e.detail.value != '') {
      this.setData({
        money: e.detail.value
      })
      this.getTax();
    } else {
      // this.setData({
      //   money: 0
      // })
      // wx.showModal({
      //   content: '请输入余额',
      //   showCancel: false,
      //   success: function (res) {
      //     if (res.confirm) {
      //     }
      //   }
      // });
    }
    if (parseFloat(e.detail.value > 50000)){
      wx.showModal({
        content: '提现金额不能超过5万',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          }
        }
      });
    }
    if (parseFloat(e.detail.value) > parseFloat(this.data.balanceBefore)) {
      wx.showModal({
        content: '余额不足',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          }
        }
      });
    }else{
      // this.getTax();
    }
  },
  // remark
  getremark:function(e){
    this.setData({
      remark: e.detail.value
    })
  },
  //计算个人税
  getTax: function () {
    const _this = this;
    if(_this.data.money != ''){
      wx.request({
        url: app.globalData.path + 'services/app/applets/IndividualTax',
        data: {
          money: _this.data.money
        },
        method: 'POST',
        header: {
          'content-type': 'application/json', // 默认值
          'Authorization': 'Bearer ' + app.globalData.authorization
        },
        success: function (res) {
          console.log(res.data);
          if (res.data.success) {
            _this.setData({
              tax: res.data.result
            })
          } else {
            wx.showToast({
              title: res.data.error.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  makesure: function () {
    if (this.data.taxshow) {
      wx.reLaunch({
        url: '../result/result?state=' + this.data.state+'&from=withdraw'
      })
      this.setData({
        showModalStatus: false
      })
    }else{
      wx.reLaunch({
        url: '../result/result?state=' + this.data.state + '&from=transferother'
      })
      this.setData({
        showModalStatus: false
      })
    }
  },
  // 发送验证码
  sendCode: function () {
    const _this = this;
    this.settimeD();
    this.setData({
      disabled: true
    })
    wx.request({
      url: app.globalData.path + 'services/app/applets/SendSMSCode',
      data: {
        phoneNumber: _this.data.phoneNumberAll,
        SendType: 3
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data)
        // console.log(res.data.result.securityCode)
        if (res.data.success) {
          if (res.data.result.securityCode){
            _this.setData({
              securityCode: res.data.result.securityCode
            })
            if (res.data.result.sendResultType == 2) {
              wx.showToast({
                title: '手机号不存在',
                icon: 'none',
                duration: 2000
              })
            } else if (res.data.result.sendResultType == 3) {
              wx.showToast({
                title: '操作频繁',
                icon: 'none',
                duration: 2000
              })
            } else if (res.data.result.sendResultType == 4 || res.data.result.sendResultType == 5) {
              wx.showToast({
                title: '短信模板错误',
                icon: 'none',
                duration: 2000
              })
            }
          }
        } else {
          wx.showToast({
            title: '发送失败，请重试',
            icon: 'none',
            duration: 2000
          })
          interval = clearInterval(interval)
          _this.setData({
            time: '重新发送',
            currentTime: 61,
            disabled: false
          })
        }
      }
    })
  },
  //倒计时
  settimeD: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        interval = clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },
  getpayD: function (e) {
    this.setData({
      payPassWord: e.detail.value
    })
    // wx.hideKeyboard();
  },
  getcodeD: function (e) {
    this.setData({
      code: e.detail.value
    })
    // wx.hideKeyboard();
  },
  // 转账
  transferA: function () {
    const _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/TransferAccount',
      data: {
        money: Number(_this.data.money),
        payPassWord: _this.data.payPassWord,
        remark:_this.data.remark,
        code:_this.data.code,
        securityCode: _this.data.securityCode,
        userId: _this.data.otherId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if(res.data.success){
          _this.setData({
            state: true
          })
          _this.makesure();
        }else{
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
          _this.setData({
            disabled1: true
          })
          // _this.setData({
          //   showModalStatus: false
          // })
          // _this.makesure();
        }
      }
    })
  },
  // 到银行卡
  transferB: function () {
    const _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/TransferBankCard',
      data: {
        money: _this.data.money,
        payPassWord: _this.data.payPassWord,
        remark: _this.data.remark,
        code: _this.data.code,
        securityCode: _this.data.securityCode,
        userId: _this.data.otherId,
        tax:_this.data.tax
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if(res.data.success){
          _this.setData({
            state: true
          })
          _this.makesure();
        }else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
          _this.setData({
            showModalStatus: false
          })
          // _this.makesure();
        }
      }
    })
  },
  // 确定
  surebtn:function(){
    this.setData({
      disabled1:false
    })
    if (this.data.taxshow){
      this.transferB();
    }else{
      this.transferA();
    }
  },
  // 过滤器
  numFilter: function (val) {
    // 截取当前数据到小数点后两位
    // let realVal = Number(val).toFixed(2)
    // num.toFixed(2)获取的是字符串
    // return Number(realVal)
    if (val || val==0) {
      val = val.toString().replace(/\$|\,/g, '');
      if (isNaN(val)) {
        val = "0";
      }
      let sign = (val == (val = Math.abs(val)));
      val = Math.floor(val * 100 + 0.50000000001);
      let cents = val % 100;
      val = Math.floor(val / 100).toString();
      if (cents < 10) {
        cents = "0" + cents
      }
      for (var i = 0; i < Math.floor((val.length - (1 + i)) / 3); i++) {
        val = val.substring(0, val.length - (4 * i + 3)) + ',' + val.substring(val.length - (4 * i + 3));
      }
      return (((sign) ? '' : '-') + val + '.' + cents);
    }
  },
})